package com.kusiri.kframework
{
	
	import com.kusiri.kframework.action.KfNewUiComponentAction;
	import com.kusiri.kframework.component.KfComponentFactory;
	import com.kusiri.kframework.ui.*;
	
	import mx.core.Application;
	import mx.core.Container;
	import mx.formatters.DateFormatter;
	import mx.managers.CursorManager;
	import mx.resources.ResourceManager;
	
	/**
	 * The main application class.
	 */
	public class KfApplication implements IKfApplication
	{

		/**
		 * Singleton class
		 */

		private static var instance:KfApplication;
		public static function getInstance() : KfApplication
		{
			if (instance==null) instance = new KfApplication();
			return instance;
		}

		/**
		 * Prana id string constants
		 */
		public const UI_CONTAINER_ID : String = 'kf-ui-container';
		public const INTIIAL_UI_ID : String = 'kf-initial-ui';


		/**
		 * Instance vars/functions
		 */
		
		private var mainModule : KfContext;
		
		/**
		 * What the UI container is contained by
		 */
		public var uiContainerParent:Container = Application.application as Container;
		
		/**
		 * The UI container
		 */
		private var _uiContainer : IKfUiContainer;
		
		public function get uiContainer() : IKfUiContainer
		{
			return _uiContainer;
		}
		
		/**
		 * Global date formatter
		 */
		public var dateFormatter : DateFormatter;

		public function KfApplication() : void
		{
			instance = this;
			dateFormatter = new DateFormatter();
			dateFormatter.formatString = ResourceManager.getInstance().getString('SharedResources', 'dateFormat');
			mainModule = new KfContext();
		}
		
		/**
		 * Called to kick off setting up the application
		 */
		public function initialize( config:* ) : void
		{
			mainModule.initialize(config, initializeUiContainer);
		} 

		/**
		 * Initialises the UI Container
		 */
		private function initializeUiContainer() : void
		{
			// create the application from the configuration + prana objects
			_uiContainer = mainModule.getObject(UI_CONTAINER_ID) as IKfUiContainer;
			_uiContainer.initialize(uiContainerParent, initializeUi);
		}	
		
		/**
		 * Initialises the UI
		 */
		private function initializeUi(e:*=null) : void
		{
			
			uiContainer.enabled = false;
			
			var initialUi : KfInitialUi = mainModule.getObject(INTIIAL_UI_ID) as KfInitialUi;
			
			var kfUiComponents : Array = initialUi.intialComponents;
			for each ( var conf : * in kfUiComponents)
			{
				var obj : Object;
				if (conf is String)
				{
					obj = new Object();
					obj.name = conf;
					obj.options = null;
				}
				else
				{
					obj = conf;
				}
				
				var action : KfNewUiComponentAction = new KfNewUiComponentAction();
				action.uiComponentFactory = new KfComponentFactory();
				action.uiComponentFactory.componentId = obj.name;
				action.uiComponentFactory.module = mainModule;
				action.uiComponentOptions = obj.options;
				action.execute();
			}
			uiContainer.enabled = true;
		}
		
		/**
		 * Called when we are waiting for something
		 */
		public function waitingForSomething() : void
		{
			CursorManager.setBusyCursor();
		}
		
		/**
		 * Called when we are waiting for something
		 */
		public function finishedWaitingForSomething() : void
		{
			CursorManager.removeBusyCursor();
		}
		
	}
}